package com.example.messageservice.service;

import com.example.messageservice.client.UserClient;
import com.example.userservice.entity.User;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MessageService {

    @Autowired
    private UserClient userClient;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    public List<User> getAllUsers() {
        return userClient.getAllUsers();
    }

    public void sendMessageToAllUsers(String message) {
        List<User> users = getAllUsers();
        for (User user : users) {
            rocketMQTemplate.convertAndSend("message-topic", "Sending message to user: " + user.getName() + ", Email: " + user.getEmail() + ", Message: " + message);
        }
    }
}